package com.chat.business;

import com.chat.data.MessageItem;
import com.chat.data.MessageType;
import com.chat.data.UserInfoBaseItem;
import com.chat.data.UserInfoDetItem;
import com.chat.util.ChatConstants;
import com.uday.frmwrk.data.BaseRequestItem;
import com.uday.frmwrk.data.BaseResponseItem;
import com.uday.frmwrk.service.FlexSessionActivity;

import flex.messaging.MessageBroker;
import flex.messaging.MessageDestination;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.services.MessageService;
import flex.messaging.util.UUIDUtils;

public class LogoutActivity extends FlexSessionActivity {

	public boolean process(BaseRequestItem objBaseRequestItem,
			BaseResponseItem objBaseResponseItem) throws Exception {
		UserInfoDetItem objUserInfoItem = (UserInfoDetItem) objBaseRequestItem
				.getObjReqData();
		logoutUser(objUserInfoItem);
		return true;
	}

	public static UserInfoBaseItem logoutUser(
	        UserInfoDetItem objUserInfoDetItem) {
		System.out.println("Logging out user : " + objUserInfoDetItem.getLngUserID());
		int intUserIndex = LoginActivity.lstUserDetInfo.indexOf(objUserInfoDetItem);
		
		if(intUserIndex != -1){
		    objUserInfoDetItem = LoginActivity.lstUserDetInfo.get(intUserIndex);
	        LoginActivity.lstUserBaseInfo.remove(objUserInfoDetItem);
	        LoginActivity.lstUserDetInfo.remove(objUserInfoDetItem);

	        String strMsgQueID = LoginActivity.mapUserIDQue.get(objUserInfoDetItem
	                .getLngUserID());
	        LoginActivity.mapUserIDQue.remove(objUserInfoDetItem
	                .getLngUserID());
	        MessageBroker broker = MessageBroker.getMessageBroker(null);
	        MessageService service = (MessageService) broker
	                .getService(ChatConstants.MESSAGE_SERVICE);
	        
	        String clientID = UUIDUtils.createUUID();
	        AsyncMessage msg = new AsyncMessage();
	        msg.setClientId(clientID);
	        msg.setMessageId(UUIDUtils.createUUID());
	        msg.setTimestamp(System.currentTimeMillis());
	        msg.setDestination(strMsgQueID);

	        MessageItem objMessageItem = new MessageItem();
	        objMessageItem
	                .setStrMessageType(MessageType.LOGOUT_MESSAGE);
            msg.setBody(objMessageItem);

            if (broker.getService(ChatConstants.MESSAGE_SERVICE)
                    .getDestination(strMsgQueID) != null) {
                broker.routeMessageToService(msg, null);
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
	        MessageDestination destination = (MessageDestination) service
	                .getDestination(strMsgQueID);
	        destination.stop();
	        service.removeDestination(strMsgQueID);
	        //service.removeDestination(strMsgQueID);
	        FriendListServiceImpl.blnRefreshList = true;
	        
	        System.out.println("Count of users currently online " + LoginActivity.lstUserDetInfo.size());

	        //TODO Inform friends
		}
		
		return objUserInfoDetItem;
	}

}
